﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace FormItem
{
    public partial class FormItem : UserControl
    {
        public delegate void addDelegate(Label lbl);
        public addDelegate add;
        public addDelegate del;
        private void dels(Label lbl)
        {
            this.Controls.Remove(lbl);
        }
        private void adds(Label lbl)
        {
            this.Controls.Add(lbl);
        }
        //字体颜色
        private Color textColor;
        public Color TextColor
        {
            get
            {
                return textColor;
            }
            set
            {
                textColor = value;
                lblText.ForeColor = value;
            }
        }

        //内容
        private String textData = "text";
        public String TextData
        {
            get
            {
                return textData;
            }
            set
            {
                textData = value;
                Thread t = new Thread(new ParameterizedThreadStart(changeData));
                t.Start(textData);
            }
        }
        //字体
        private Font textFont;
        public Font TextFont
        {
            get
            {

                return textFont;
            }
            set
            {
                textFont = value;
                lblText.Font = value;
            }
        }

        //下划线宽度
        private int lineWidth = 1;

        public int LineWidth
        {
            get
            {
                return lineWidth;
            }
            set
            {
                lineWidth = value;
                this.Invalidate();
            }
        }

        //下划线颜色
        private Color lineColor = Color.Black;

        public Color LineColor
        {
            get
            {
                return lineColor;
            }
            set
            {
                lineColor = value;
                this.Invalidate();
            }
        }

        //字于下划线的间距
        private int lineSpace = 1;

        public int LineSpace
        {
            get
            {
                return lineSpace;
            }
            set
            {
                lineSpace = value;
                this.Invalidate();
            }
        }

        public FormItem()
        {
            InitializeComponent();
        }

        private void FormItem_Load(object sender, EventArgs e)
        {
            add = adds;
            del = dels;
        }

        private void FormItem_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = this.CreateGraphics();
            Pen p = new Pen(lineColor, lineWidth);
            p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;

            g.DrawLine(p, 0, lblText.Size.Height + lineSpace, this.Size.Width, lblText.Size.Height + lineSpace);
        }

        private void lblText_Resize(object sender, EventArgs e)
        {
            int x = (this.Size.Width - lblText.Size.Width) / 2;
            Point p = new Point(x, 0);
            lblText.Location = p;

            Size size = new Size();
            size.Height = lblText.Size.Height + lineSpace + lineWidth;
            size.Width = this.Size.Width;
            this.Size = size;
        }

        #region 改变值时的翻动效果
        private void changeData(Object obj)
        {
            Control.CheckForIllegalCrossThreadCalls = false;

            Point p = new Point();
            p.Y = 0;
            p.X = lblText.Location.X;

            Label temp = new Label();
            temp.Font = lblText.Font;
            temp.Location = p;
            temp.Text = lblText.Text;
            temp.ForeColor = lblText.ForeColor;

            lblText.Text = obj.ToString();
            p = new Point();
            p.Y = 0 - lblText.Size.Height;
            p.X = lblText.Location.X;
            lblText.Location = p;

            if (this.InvokeRequired)
                this.Invoke(add,new Object[]{temp});

            for (int i = 0; lblText.Location.Y < 0; i++)
            {
                Point pT = new Point();
                pT.X = temp.Location.X;
                pT.Y = temp.Location.Y + i;
                temp.Location = pT;

                Point pL = new Point();
                pL.X = lblText.Location.X;
                pL.Y = lblText.Location.Y + i;
                lblText.Location = pL;

                Thread.Sleep(100);
            }
            del(temp);
        }
        #endregion
    }
}
